﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace ClinicSystem.FormPage
{
	public partial class printSet : Form
	{
		private PrintDocument printDocument = new PrintDocument();
		float printerDpiX;//打印机每英寸像素
		float printerDpiY;
		float paperX = 0;//打印实际宽高
		float paperY = 0;
		public printSet()
		{
			InitializeComponent();
			InitView();


			foreach (string printer in PrinterSettings.InstalledPrinters)
			{
				comboBox_printerList.Items.Add(printer);
			}
			if (comboBox_paperSize.SelectedIndex == 6)
			{
				numericUpDown_paperX.Enabled = true;
				numericUpDown_paperY.Enabled = true;
			}
			else
			{
				numericUpDown_paperX.Enabled = false;
				numericUpDown_paperY.Enabled = false;
			}

			comboBox_printerList.Text = printDocument.PrinterSettings.PrinterName;

			comboBox_printerList.TextChanged += (s, e) =>//打印机选择
			{
				printDocument.PrinterSettings.PrinterName = comboBox_printerList.Text;
			};
			//纸张选择
			comboBox_paperSize.SelectedIndexChanged += (s, e) =>
			{

				if (comboBox_paperSize.SelectedIndex == 6)
				{
					numericUpDown_paperX.Enabled = true;
					numericUpDown_paperY.Enabled = true;
					paperX = (float)numericUpDown_paperX.Value;
					paperY = (float)numericUpDown_paperY.Value;
				}
				else
				{
					numericUpDown_paperX.Enabled = false;
					numericUpDown_paperY.Enabled = false;

				}

			};
			printDocument.PrintPage += PrintDocument_PrintPage;
		}

		private void InitView()
		{
			string name = GlobalVar.temp[0];
			string gender = GlobalVar.temp[1];
			string age = GlobalVar.temp[2];
			string medicalID = GlobalVar.temp[3];
			string telnumber = GlobalVar.temp[4];
			string address = GlobalVar.temp[5];
			string mainsuit = GlobalVar.temp[7];
			string history = GlobalVar.temp[8];
			string current = GlobalVar.temp[9];
			string pulse = GlobalVar.temp[10];
			string tougne = GlobalVar.temp[11];
			string diagnoseResult = GlobalVar.temp[12];
			int w = pictureBox_view.Width;
			int h = pictureBox_view.Height;
			Bitmap bmp = new Bitmap(w, h);
			// 创建 Graphics 对象，用于在 Bitmap 上绘制
			Graphics graphics = Graphics.FromImage(bmp);

			// 设置字体、颜色等文字样式
			Font font = new Font("宋体", 9);
			Font titleFont = new Font("楷体", 12);
			Brush brush = Brushes.Black;
			PointF point = new PointF(w - 20, 10); // 文字起始位置
			graphics.DrawString("松隐堂中医诊所代煎单", font, brush, point);
			graphics.DrawString("姓名:" + name, font, brush, 20, 20);
			graphics.DrawString("性别:" + gender, font, brush, 50, 20);
			graphics.DrawString("年龄:" + age, font, brush, 70, 20);
			graphics.DrawString("病历号:" + medicalID, font, brush, 100, 100);

			pictureBox_view.Image = bmp;
			pictureBox_view.Width = bmp.Width;
		}

		private void choosePaperSize(PrintPageEventArgs e)
		{
			// 计算打印机的每英寸点数（DPI）
			printerDpiX = e.Graphics.DpiX;
			printerDpiY = e.Graphics.DpiY;

			if (comboBox_paperSize.SelectedIndex == 0)
			{
				paperX = 297;
				paperY = 420;
				printDocument.DefaultPageSettings.PaperSize = new PaperSize("A3", Convert.ToInt32((paperX / 25.4) * 100 * printerDpiX), Convert.ToInt32((paperY / 25.4) * 100 * printerDpiY));
			}
			else if (comboBox_paperSize.SelectedIndex == 1)
			{
				paperX = 210;
				paperY = 297;
				printDocument.DefaultPageSettings.PaperSize = new PaperSize("A4", Convert.ToInt32((paperX / 25.4) * 100 * printerDpiX), Convert.ToInt32((paperY / 25.4) * 100 * printerDpiY));
			}
			else if (comboBox_paperSize.SelectedIndex == 2)
			{
				paperX = 148;
				paperY = 210;
				printDocument.DefaultPageSettings.PaperSize = new PaperSize("A5", Convert.ToInt32((paperX / 25.4) * 100 * printerDpiX), Convert.ToInt32((paperY / 25.4) * 100 * printerDpiY));
			}
			else if (comboBox_paperSize.SelectedIndex == 3)
			{
				paperX = 250;
				paperY = 353;
				printDocument.DefaultPageSettings.PaperSize = new PaperSize("B4", Convert.ToInt32((paperX / 25.4) * 100 * printerDpiX), Convert.ToInt32((paperY / 25.4) * 100 * printerDpiY));
			}
			else if (comboBox_paperSize.SelectedIndex == 4)
			{
				paperX = 176;
				paperY = 250;
				printDocument.DefaultPageSettings.PaperSize = new PaperSize("B5", Convert.ToInt32((paperX / 25.4) * 100 * printerDpiX), Convert.ToInt32((paperY / 25.4) * 100 * printerDpiY));
			}
			else if (comboBox_paperSize.SelectedIndex == 5)
			{
				paperX = 125;
				paperY = 176;
				printDocument.DefaultPageSettings.PaperSize = new PaperSize("B6", Convert.ToInt32((paperX / 25.4) * 100 * printerDpiX), Convert.ToInt32((paperY / 25.4) * 100 * printerDpiY));
			}
			else if (comboBox_paperSize.SelectedIndex == 6)
			{
				paperX = 125;
				paperY = 176;
				printDocument.DefaultPageSettings.PaperSize = new PaperSize("Custom", Convert.ToInt32((paperX / 25.4) * 100 * printerDpiX), Convert.ToInt32((paperY / 25.4) * 100 * printerDpiY));
			}
		}
		private void printContent(Graphics graphics, RectangleF printArea)
		{
			string name = GlobalVar.temp[0];
			string gender = GlobalVar.temp[1];
			string age = GlobalVar.temp[2];
			string medicalID = GlobalVar.temp[3];
			string telnumber = GlobalVar.temp[4];
			string address = GlobalVar.temp[5];
			string mainsuit = GlobalVar.temp[7];
			string history = GlobalVar.temp[8];
			string current = GlobalVar.temp[9];
			string pulse = GlobalVar.temp[10];
			string tougne = GlobalVar.temp[11];
			string diagnoseResult = GlobalVar.temp[12];
			Font font = new Font("宋体", 9);
			Font titleFont = new Font("楷体", 12);
			Brush brush = Brushes.Black;
			StringFormat stringFormat = new StringFormat
			{
				Alignment = StringAlignment.Center,

			};
			graphics.DrawString("松隐堂中医诊所代煎单", titleFont, brush, printArea, stringFormat);

			font.Dispose();
			titleFont.Dispose();
			stringFormat.Dispose();
		}
		private void PrintDocument_PrintPage(object sender, PrintPageEventArgs e)
		{
			choosePaperSize(e);
			printContent(e.Graphics, e.MarginBounds);

		}

		private void button_print_Click(object sender, EventArgs e)
		{
			using (Bitmap bitmap = new Bitmap((int)paperX, (int)paperY))
			{
				using (Graphics g = Graphics.FromImage(bitmap))
				{
					g.Clear(Color.White);
					var printArgs = new PrintPageEventArgs(g, new Rectangle(0, 0, (int)paperX, (int)paperY),
						new Rectangle(0, 0, (int)paperX, (int)paperY), printDocument.DefaultPageSettings);
					PrintDocument_PrintPage(printDocument, printArgs);
				}
				pictureBox_view.SizeMode = PictureBoxSizeMode.Zoom;
				pictureBox_view.Image = new Bitmap(bitmap); // 深拷贝避免释放
			}
			try
			{
				printDocument.Print();
				this.Dispose();
			}
			catch(Exception ex)
			{
				MessageBox.Show(ex.ToString() + "打印失败");
			}

		}



		private void button_getPrinter_Click(object sender, EventArgs e)
		{
			comboBox_printerList.Items.Clear();
			foreach (string printer in PrinterSettings.InstalledPrinters)
			{
				comboBox_printerList.Items.Add(printer);
			}
			comboBox_printerList.Text = printDocument.PrinterSettings.PrinterName;
		}
	}
}
